﻿#include <QApplication>
#include <QMainWindow>
#include <QToolBar>
#include <QAction>
#include <QIcon>
#include <QMessageBox>

class MyClass : public QMainWindow {
    Q_OBJECT

public:
    MyClass(QWidget *parent = nullptr) : QMainWindow(parent) {
        // 创建工具栏
        QToolBar *toolbar = new QToolBar("MyToolbar", this);
        toolbar->setIconSize(QSize(32, 32));  // 设置图标大小
       toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        // 添加打开按钮
        QAction *actionOpen = toolbar->addAction(QIcon(":/images/open.png"), "Open");
        connect(actionOpen, &QAction::triggered, this, &MyClass::openFile);

        // 添加保存按钮
        QAction *actionSave = toolbar->addAction(QIcon(":/images/Save.png"), "Save");
        connect(actionSave, &QAction::triggered, this, &MyClass::saveFile);

        // 添加分隔符
        toolbar->addSeparator();

        // 添加自定义部件
        QWidget *customWidget = new QWidget;
        customWidget->setFixedSize(100, 30);
        toolbar->addWidget(customWidget);

        // 将工具栏添加到主窗口
        addToolBar(toolbar);
    }

private slots:
    void openFile() {
        QMessageBox::information(this, "Open", "Open file");
    }

    void saveFile() {
        QMessageBox::information(this, "Save", "Save file");
    }
};

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    MyClass window;
    window.show();

    return a.exec();
}

#include "main.moc"
